<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>addOrEdit</title>
</head>
<style>table{border: 1px solid black;}</style>
<body>
    <div>        
    <table>
        <tr><td style="font-size: 18px;">添加博客或者修改</td></tr>
        <tr>
            <td><label for="">标题：</label></td>
            <td><input type="text" placeholder="请输入商品名称" name="Title"></td>
        </tr>
        <tr>
            <td><label for="">摘要：</label></td>
            <td><input type="text" placeholder="请输入摘要" name="Abstract"></td>
        </tr>
        <tr>
            <td><label for="">内容：</label></td>
            <td><input type="text" placeholder="请输入内容" name="Content"></td>
        </tr>
        <tr>
            <td><label for="">分类：</label></td>
            <td><input type="text" placeholder="请输入分类" name="Classification"></td>
        </tr>
        <tr>
            <td><label for="">作者：</label></td>
            <td><input type="text" placeholder="请输入作者" name="Author"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消并返回" onclick="cancel()"></td>
        </tr>
    </table>
</div>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config/baseUrl.js"></script>
    <script src="./js/api.js"></script>
    <script>
        $(function () {
            let id = getUrlParam('id');
            id = id || 0;
            getblogById(id).then(res => {
                let obj = res.data;
               $('[name=Title]').val(obj.Title); 
               $('[name=Abstract]').val(obj.Abstract); 
               $('[name=Content]').val(obj.Content); 
               $('[name=Classification]').val(obj.Classification); 
               $('[name=Author]').val(obj.Author); 
            })
        })
        function getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); 
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
        function save() {
            let id = getUrlParam('id');
            let obj = {
                Title: $('[name=Title]').val(),
                Abstract: $('[name=Abstract]').val(),
                Content: $('[name=Content]').val(),
                Classification: $('[name=Classification]').val(),
                Author: $('[name=Author]').val(),
            }
            if (id) {
                updateblog(id, obj).then(res => {
                    location.href = './index.html';
                })
            } else {
                let params = {
                    Title: obj. Title,
                    Abstract: obj.Abstract,
                    Content: obj.Content,
                    Classification: obj.Classification,
                    Author: obj.Author,
                }
                addblog(params).then(res => {
                    location.href = './index.html';
                })
            }
        }
        function cancel() {
            location.href = './index.html';
        }
    </script>
</body>
</html>